--------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  X:\shared\ambulance\Work\replicationpackage\logs\CS_estimator.log
  log type:  text
 opened on:  13 Jul 2024, 07:49:21

. 
. /*******************************************************************************
> CS_estimator.do
> 
> Analyzes district-level data using the Callaway and Sant'anna estimator
> *******************************************************************************/
. 
. qui {

. 
. local pre=24

. local post=23

. 
. foreach var in PA civ crim {
  2.         qui {
  3.                 replace dropme=0
  4.                 if "`var'"=="crim" {
  5.                         local xti = "Months Since First Criminal Enforcement"
  6.                         local outlist "log_tot_pmt log_rides"
  7.                 }
  8.                 if "`var'"=="civ" {
  9.                         local xti = "Months Since First Civil Enforcement"
 10.                         local outlist "log_tot_pmt log_rides"
 11.                 }
 12.                 if "`var'"=="PA" {
 13.                         local xti = "Months Since Prior Authorization Implementation"
 14.                         replace dropme=1 if month_date<612
 15.                         local outlist "tot_pmt log_tot_pmt ambulance_id log_rides active_firms log_active_firms"
 16.                 }
 17.                 replace treat=mofd(`var'_date)
 18.                 replace treat=0 if missing(treat)
 19.                 
.                 su month_date if dropme==0
 20.                 local firsttime=`r(min)'
 21.                 local lasttime=`r(max)'
 22.                 replace dropme=1 if treat!=0 & treat<`firsttime'+`pre'-1
 23.                 replace dropme=1 if treat!=0 & treat>`lasttime'+`post'
 24.                 
.                 su treat if treat!=0 & dropme==0
 25.                 local pre_coeffs=`r(max)'-`firsttime'
 26.                 local post_coeffs=`lasttime'-`r(min)'
 27.                 local total_coeffs=`pre_coeffs'+`post_coeffs'
 28.         }
 29. 
.         forvalues cont=0/1 {
 30.                 if `cont'==0 {
 31.                         local notyet = ""
 32.                 }
 33.                 if `cont'==1 {
 34.                         local notyet = "notyet"
 35.                 }
 36.                         
.                 matrix `var'_res=[0,0,0,0,0,0,0,0,0]'
 37. 
.                 if "`var'"!="PA" | `cont'==0 {
 38.                         foreach out in `outlist' {
 39.                                 qui {
 40.                                         csdid `out' if dropme==0, ///
>                                                 ivar(dist_code) time(month_date) gvar(treat) agg(event) long `notyet'
 41.                                         estat cevent, window(0 `post')
 42.                                         matrix `var'_res=[`var'_res, r(table)]
 43.                                 
.                                 *Note csdid, long returns difference between t-1 and relevant period for the pre-period, we want the negative of this:
.                                 }
 44.                                 
.                                 if "`out'"=="log_tot_pmt" {
 45.                                         qui {
 46.                                                 replace plotnum=_n if _n<=`total_coeffs'
 47.                                                 replace point=e(b)[1,plotnum]
 48.                                                 replace se=sqrt(e(V)[plotnum,plotnum])
 49.                                                 replace ul=point+se*1.96
 50.                                                 replace ll=point-se*1.96
 51.                                                 replace plotnum=plotnum+1 if _n>=`pre_coeffs'
 52.                                                 replace plotnum=`pre_coeffs' if _n==`total_coeffs'+1
 53.                                                 replace point=0 if plotnum==`pre_coeffs'
 54.                                                 foreach bound in point ul ll {
 55.                                                                 replace `bound'=-`bound' if plotnum<`pre_coeffs'
 56.                                                 }
 57.                                                 replace plotnum=plotnum-`pre_coeffs'+`pre'
 58.                                         }
 59.                                         
.                                         sort plotnum
 60.                                         if (`cont'==0) | (`cont'==1 & "`var'"!="PA") {
 61.                                                 twoway (connected point plotnum) ///
>                                                         (rcap ul ll plotnum, color(navy)) ///
>                                                         if plotnum>=1 & plotnum<=`pre'+`post'+1, ///
>                                                         xti("`xti'") ///
>                                                         xline(25, lcolor(gs8) lpattern(dash)) ///
>                                                         xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
>                                                                 31 "6" 37 "12" 43 "18" 49 "24") ///
>                                                         yti("Total Payments in District (Logs)") ///
>                                                         yline(0, lcolor(gs8)) ylab(, angle(0)) ///
>                                                         leg(off) graphregion(color(white)) bgcolor(white)
 62.                                                 if "`var'"=="PA" {
 63.                                                         qui graph export ${outpath}Figure_A4a.pdf, as(pdf) replace // Figures A4a
 64.                                                 }
 65.                                                 if "`var'"=="civ" & `cont'==0 {
 66.                                                         qui graph export ${outpath}Figure_A4c.pdf, as(pdf) replace // Figures A4c
 67.                                                 }
 68.                                                 if "`var'"=="crim" & `cont'==0 {
 69.                                                         qui graph export ${outpath}Figure_A4d.pdf, as(pdf) replace // Figures A4d
 70.                                                 }
 71.                                                 if "`var'"=="civ" & `cont'==1 {
 72.                                                         qui graph export ${outpath}Figure_A7a.pdf, as(pdf) replace // Figures A7a
 73.                                                 }
 74.                                                 if "`var'"=="crim" & `cont'==1 {
 75.                                                         qui graph export ${outpath}Figure_A7b.pdf, as(pdf) replace // Figures A7b
 76.                                                 }
 77.                                         }
 78.                                         foreach repvar in plotnum point se ul ll {
 79.                                                 qui replace `repvar'=.
 80.                                         }
 81.                                 }
 82.                                 if "`out'"=="log_active_firms" & `cont'==0 {
 83.                                         qui {
 84.                                                 replace plotnum=_n if _n<=`total_coeffs'
 85.                                                 replace point=e(b)[1,plotnum]
 86.                                                 replace se=sqrt(e(V)[plotnum,plotnum])
 87.                                                 replace ul=point+se*1.96
 88.                                                 replace ll=point-se*1.96
 89.                                                 replace plotnum=plotnum+1 if _n>=`pre_coeffs'
 90.                                                 replace plotnum=`pre_coeffs' if _n==`total_coeffs'+1
 91.                                                 replace point=0 if plotnum==`pre_coeffs'
 92.                                                 foreach bound in point ul ll {
 93.                                                                 replace `bound'=-`bound' if plotnum<`pre_coeffs'
 94.                                                 }
 95.                                                 replace plotnum=plotnum-`pre_coeffs'+`pre'
 96.                                         }
 97.                                         
.                                         sort plotnum
 98.                                         twoway (connected point plotnum) ///
>                                                 (rcap ul ll plotnum, color(navy)) ///
>                                                 if plotnum>=1 & plotnum<=`pre'+`post'+1, ///
>                                                 xti("`xti'") ///
>                                                 xline(25, lcolor(gs8) lpattern(dash)) ///
>                                                 xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
>                                                         31 "6" 37 "12" 43 "18" 49 "24") ///
>                                                 yti("Active Firms in District (Logs)") ///
>                                                 yline(0, lcolor(gs8)) ylab(, angle(0)) ///
>                                                 leg(off) graphregion(color(white)) bgcolor(white)
 99.                                         qui graph export ${outpath}Figure_A4b.pdf, as(pdf) replace // Figure A4b
100.                                         foreach repvar in plotnum point se ul ll {
101.                                                 qui replace `repvar'=.
102.                                         }
103.                                 }
104.                         }
105.                 
.                         if "`var'"=="PA" {
106.                                 qui putexcel set "${outpath}Table_A2.xlsx", replace
107.                                 matrix rownames `var'_res = b se z pvalue ll ul df crit eform
108.                                 matrix colnames `var'_res = blank tot_pmt log_tot_pmt ambulance_id log_rides active_firms log_active_firms 
109.                         }
110.                         else if `cont'==0 {
111.                                 qui putexcel set "${outpath}Table_A3_`var'.xlsx", replace
112.                                 matrix rownames `var'_res = b se z pvalue ll ul df crit eform
113.                                 matrix colnames `var'_res = blank log_tot_pmt log_rides 
114.                         }
115.                         else {
116.                                 qui putexcel set "${outpath}Table_A14_`var'.xlsx", replace
117.                                 matrix rownames `var'_res = b se z pvalue ll ul df crit eform
118.                                 matrix colnames `var'_res = blank log_tot_pmt log_rides 
119.                         }
120.                         qui putexcel A1=matrix(`var'_res), names // Tables A2, A3, and A14
121.                 }
122.         }
123. }

. 
. log close
      name:  <unnamed>
       log:  X:\shared\ambulance\Work\replicationpackage\logs\CS_estimator.log
  log type:  text
 closed on:  13 Jul 2024, 12:06:59
--------------------------------------------------------------------------------------------------------------------------------------------------------
